/**
 * ExileServer_system_simulationMonitor_thread_toggleSimulation
 *
 * Exile Mod
 * www.exilemod.com
 * © 2015 Exile Mod Team
 *
 * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. 
 * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
 */
 
private["_vehicle","_simulationShouldBeEnabled","_position","_timeStamp","_simulatedZombie"];

_simulatedZombie = AfterlifeServerSimulatedUnit; 

{
	_nearVehicles = getpos _x nearEntities [["Air","Car",'Exile_Unit_Player'], 600];
	_nearPlayers = false;
	{
		private "_x";
		if (isPlayer _x) then
		{
			_nearPlayers = true;

		}; 	
	} forEach _nearVehicles;

	if (_nearPlayers ) then
	{
		_x enableSimulationGlobal true;
		_x hideObjectGlobal false;
		_simulatedZombie = _simulatedZombie - [_x]; 
	}
	else
	{
		_x enableSimulationGlobal false;
		_x hideObjectGlobal true;
	};

} forEach _simulatedZombie;

{
	_vehicle = _x;
	_simulationShouldBeEnabled = false;
	if !(crew _vehicle isEqualTo []) then 
	{
		_simulationShouldBeEnabled = true;	
	}
	else 
	{
		_position = getPos _vehicle;
		if (_position select 2 > 20) then
		{
			_simulationShouldBeEnabled = true;
		}
		else 
		{
			if ([_position, 250] call ExileClient_util_world_isAlivePlayerInRange) then
			{
				_simulationShouldBeEnabled = true;
			};
		};
	};
	if (_simulationShouldBeEnabled) then
	{
		if !(simulationEnabled _vehicle) then
		{
			_vehicle enableSimulationGlobal true;
		};
	}
	else 
	{
		if (simulationEnabled _vehicle) then
		{
			_vehicle enableSimulationGlobal false;
		};
	};
}
forEach ExileSimulationMonitoredVehicles;

{
	_timeStamp = _x getVariable "AfterlifeServerZombieSpawnedAt";

	if !(isNil "_timeStamp") then
	{
		if (time >= _timeStamp) then
		{
			deleteVehicle _x;
			AfterlifeServerSimulatedUnit = AfterlifeServerSimulatedUnit - [_x];
		};
	};		
} forEach AllUnits;

true